package cn.sfinger.huifu.p2p.util.security;

import java.math.BigInteger;

public class KeyTool {
	private String publicKey;
	private String privateKey;
	
	public KeyTool(){
		
	}
	
	public KeyTool(String priKey, String pubKey){
		this.publicKey= pubKey;
		this.privateKey= priKey;
	}
	
	/**
	 * @desc sign 
	 * @param message
	 * @return
	 */
	public String signMessage(String message){
		PrivateKey priKey= new PrivateKey(privateKey);
		byte[] paramArrayOfByte= message.getBytes();
	    int i = paramArrayOfByte.length;
	    byte[] arrayOfByte1 = paramArrayOfByte;
	    byte[] arrayOfByte2 = new byte[''];

	    RSAEncrypt localRSAEncrypt = new RSAEncrypt(priKey);

	    ShaHash localShaHash = new ShaHash();
	    localShaHash.add(arrayOfByte1, 0, i);
	    arrayOfByte2 = localShaHash.get();

	    String str = localRSAEncrypt.EncryptMessage(arrayOfByte2);
	    String ChkValue = str.toUpperCase();
		return ChkValue;
	}
	
	public boolean verifySign(String message, String sign){
		PrivateKey priKey= new PrivateKey(publicKey,false);
		
	    byte[] arrayOfByte3 = message.getBytes();
	    int i = arrayOfByte3.length;
	    
	    ShaHash localShaHash = new ShaHash();
	    localShaHash.add(arrayOfByte3, 0, i);
	    byte[] arrayOfByte2 = localShaHash.get();

	    String str1 = decryptMessage(sign, priKey);
	    String str2 = toStringBlock(arrayOfByte2);
	    return str2.equalsIgnoreCase(str1);
	    
	}

	public static String decryptMessage(String paramString, PrivateKey priKey){
	    BigInteger localBigInteger1;
	    try
	    {
	      BigInteger localBigInteger2 = new BigInteger(paramString, 16);
	      BigInteger localBigInteger3 = new BigInteger(1, priKey.Modulus);
	      BigInteger localBigInteger4 = new BigInteger("010001", 16);
	      localBigInteger1 = localBigInteger2.modPow(localBigInteger4, localBigInteger3);
	    }
	    catch (NumberFormatException localNumberFormatException)
	    {
	      return "-1";
	    }
	    String str= "";
	    for (str = localBigInteger1.toString(16); str.length() < 256; str = "0" + str);
	    return str;
	  }
	
	public static String toStringBlock(byte[] paramArrayOfByte){
	    return toStringBlock(paramArrayOfByte, 0, paramArrayOfByte.length);
	}

	private static String toStringBlock(byte[] paramArrayOfByte, int paramInt1, int paramInt2){
	    String str = "0123456789abcdef";
	    StringBuffer localStringBuffer = new StringBuffer();
	    for (int i = paramInt1; i < paramInt1 + paramInt2; i++)
	    {
	      localStringBuffer.append(str.charAt(paramArrayOfByte[i] >>> 4 & 0xF));
	      localStringBuffer.append(str.charAt(paramArrayOfByte[i] & 0xF));
	    }

	    return String.valueOf(String.valueOf(localStringBuffer));
	}
	
	public String getPublicKey() {
		return publicKey;
	}
	public void setPublicKey(String publicKey) {
		this.publicKey = publicKey;
	}
	public String getPrivateKey() {
		return privateKey;
	}
	public void setPrivateKey(String privateKey) {
		this.privateKey = privateKey;
	}
}
